package file;

import java.io.*;
import java.util.Scanner;

public class Demo11 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        //1.输入路径,合法性判定
        System.out.println("请输入要复制的文件路径: ");
        String src = scanner.next();
        File srcFile = new File(src);
        if (!srcFile.isFile()){
            System.out.println("您要复制的源文件路径是非法的");
            return;
        }
        System.out.println("请输入要复制到的目标路径:");
        String dest = scanner.next();
        File destFile = new File(dest);
        //不要求目标文件本身存在,要求其所在的目录存在
        if (!destFile.getParentFile().isDirectory()){
            System.out.println("您要复制到的目标路径是非法的");
            return;
        }

        //2.进行复制操作的过程,按照字节流打开:(文本二进制都可以按照字节流方式操作)
        try (InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(destFile)){
            while (true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                System.out.println("n = " +n);
                if (n ==-1){
                    System.out.println("读取到eof,循环结束");
                    break;
                }
                //把读到的内容写入
                outputStream.write(buffer,0,n);

            }
        }

    }
}
